<?php include("../../includes/includes.php"); ?>
<?
connect($config);
$players = getPlayerReplacementNames();
$textDump = $_POST['textDump'];
$lines = preg_split("%\n%", $textDump);
$count = count($lines);
$round = getCurrentRound();
$team = "";
$mapKey = "";
for ($i = 0; $i < $count; $i++) {
	if (strpos($lines[$i], ":") == "") {
		$team = trim($lines[$i]);
		$mapKey = getAFLTeamMapKey($team);
	}
	else {
		$spaceIndex = strpos($lines[$i], " ");
		$linePosition = str_replace(":", "", trim(substr($lines[$i], 0, $spaceIndex)));
		$lineCells = split(", ", trim(substr($lines[$i], $spaceIndex)));
		$countLineCells = count($lineCells);
		for ($j = 0; $j < $countLineCells; $j++) {
			$spaceIndex = strpos($lineCells[$j], " ");
	    	$firstName = trim(substr($lineCells[$j], 0, $spaceIndex));
	    	$surname = trim(substr($lineCells[$j], $spaceIndex + 1));
	    	$count2 = count($players);
	    	for ($k = 0; $k < $count2; $k++) {
		    	if ($players[$k]->firstName == $firstName && $players[$k]->surname == $surname && $players[$k]->team == getAFLTeam($mapKey)) {
		    		$firstName = $players[$k]->requiredFirstName;
	    			$surname = $players[$k]->requiredSurname;
	    		
	    			break;
	    		}
	    	}
	    	
	    	$playerId = getPlayerID($firstName, $surname, $team);
	    	$position = getPosition($linePosition, $j);
			if ($playerId == null) {
		    	echo "ERROR: PLAYER NOT FOUND:" . $firstName . " " . $surname . " (" . $team . ")<br>"; 
		    }
		    else {
		    	insertSelectTeamPlayer($playerId, $round, $position);
	    	}
		}		
	}
}

mysql_close();

echo "</table><br><b>COMPLETED!!</b>";
?>